﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using Things.Common;

namespace Things.Web
{
    public class PageUtility: Page
    {
       

        public string UploadFile(FileUpload FileUpload1)
        {
            string imageURL = string.Empty;
            try
            {
                string filePath = FileUpload1.PostedFile.FileName;
                string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
                string folderName = CreateFolder();
                string serverPath = Server.MapPath("~/images/" + folderName + "/") + fileName;
                FileUpload1.PostedFile.SaveAs(serverPath);

                imageURL = "/images/" + folderName + "/" + fileName;
            }
            catch
            {
                imageURL = string.Empty;
            }

            return imageURL;
        }

        public string CreateFolder()
        {
            DateTime   dt=System.DateTime.Now;
            string dirName = HttpContext.Current.Server.MapPath("~/images/") + dt.ToString("yyyyMMdd");
            string folderName = dt.ToString("yyyyMMdd");
            try 
            { 
                  if(System.IO.Directory.Exists(dirName) )
                  { 
                        // throw   new   Exception( "目录已存在 ");                       
                  } 
                  System.IO.Directory.CreateDirectory(dirName); 

            } 
            catch
            {
                folderName = string.Empty;
            }
            return folderName;
        }

      
    }
}